package jMiiEditor.gui.checker;

import jMiiEditor.mii.checker.MiiCheckerResult;
import java.util.List;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableColumn;

public class MiiCheckerResultTableModel
    extends AbstractTableModel
        //implements TableModelListener
{
    private static final long serialVersionUID = 1L;
    /** @serial */
    private List<MiiCheckerResult.Entry> ActionPerfomed;

    public MiiCheckerResultTableModel(
            MiiCheckerResult miiCheckerResult
            )
    {
        super();

        this.ActionPerfomed = miiCheckerResult.getActionPerfomed();
        //addTableModelListener(this);
    }

    @Override
    public String getColumnName(int column)
    {
        switch( column ) {
            case 0 : return "Description";
            default: return "Erreur";
        }
    }

    @Override
    public Class<?> getColumnClass(int columnIndex)
    {
        return String.class;
//        switch( columnIndex ) {
//            case 0 : return String.class;
//            default: return String.class;
//        }
    }

    @Override
    public int getColumnCount()
    {
        return 2;
    }

    @Override
    public int getRowCount()
    {
        return this.ActionPerfomed.size();
    }

    @Override
    public Object getValueAt( int row, int column )
    {
        MiiCheckerResult.Entry entry = this.ActionPerfomed.get( row );

        if( column == 0 ) {
            return entry.getDescription();
        }
        else if( column == 1 ) {
            if( entry.getCause() == null ) {
                return "Ok.";
            }
            else {
                return entry.getCause().getMessage();
            }
        }

        return null;
    }

//    @Override
//    public boolean isCellEditable(int rowIndex, int columnIndex)
//    {
//        if( columnIndex == 1 ) {
//            MiiCheckerProposal rowValue = this.checkResult.get( rowIndex );
//
//            if( rowValue instanceof MiiCheckerProposalAction ) {
//                System.out.println( "editable:" + rowIndex + "," + columnIndex);
//                return true;
//            }
//        }
//
//        return false;
//    }

//    @Override
//    public void setValueAt(Object aValue, int rowIndex, int columnIndex)
//    {
//        System.out.println( "setValueAt:" + aValue + " row:" + rowIndex + " col:" + columnIndex);
//
//        if( columnIndex == 1 ) {
//            MiiCheckerProposal rowValue = this.checkResult.get( rowIndex );
//
//            if( rowValue instanceof MiiCheckerProposalAction ) {
//                this.checkResultDoIt.set( rowIndex, Boolean.class.cast( aValue ) );
//            }
//        }
//    }


    public void setColumnWidth(JTable table)
    {//Not Override
        TableColumn column = null;

        for( int i = 0; i < 2; i++ ) {
            column = table.getColumnModel().getColumn( i );
            switch( i ) {
                case 0:
                    column.setPreferredWidth( 100 );
                    break;
                case 1:
                    column.setPreferredWidth( 300 );
                    break;
                default:
                    break;
            }
        }
    }

//    @Override
//    public void tableChanged( TableModelEvent event )
//    {
//        System.out.println( "tableChanged1:" + event );
//        //super.tableChanged( event );
//        int row    = event.getFirstRow();
//        int column = event.getColumn();
//        TableModel model = (TableModel)event.getSource();
//        //String columnName = model.getColumnName(column);
//        Object data = model.getValueAt(row, column);
//
//        if( data != null ) {
//            System.out.println( "tableChanged2:" + data.getClass() );
//        }
//    }

}
